﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;

/// <summary>
/// Summary description for PlayerDetailLinqProvider
/// </summary>
/// 
namespace com.ClickCricket.dal
{
    public class PlayerDetailLinqProvider : IPlayerDetailProvider
    {
        public PlayerDetailLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public PlayerDetail getPlayerDetails(int playerID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<PlayerDetail> _PDetails = new List<PlayerDetail>();
                var playerDetail = from aPDetail in dc.PlayerDetailTables
                                   where aPDetail.PlayerID == playerID
                                   select aPDetail;
                if(playerDetail == null)
                {
                    return null;
                }
                
                if(playerDetail.Count<Entity.PlayerDetailTable>() == 1)
                {
                    return extractPlayerDetail(playerDetail);
                }

                throw new Exception("Invalid Player Id " + playerID + " !");
            }
        }

        private PlayerDetail extractPlayerDetail(IQueryable<Entity.PlayerDetailTable> playerDetail)
        {
            PlayerDetail aPDetail = new PlayerDetail(
                playerDetail.Single<Entity.PlayerDetailTable>().PlayerDetailID,
                playerDetail.Single<Entity.PlayerDetailTable>().BattingStyle,
                playerDetail.Single<Entity.PlayerDetailTable>().BowlingStyle,
                playerDetail.Single<Entity.PlayerDetailTable>().FieldingPosition
                );
            return aPDetail;
        }

    }
}
